﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class frmContacto : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["cuit"] != null)
        {
           

            if (!Page.IsPostBack)
            {
                TextBox9.Text = Session["cuit"].ToString();

                this.DSEmpresa.SelectParameters["cuit"].DefaultValue = this.TextBox9.Text;
                this.DSEmpresa.DataSourceMode = SqlDataSourceMode.DataReader;
                SqlDataReader datos = (SqlDataReader)this.DSEmpresa.Select(DataSourceSelectArguments.Empty);

                if (datos.Read())
                {
                   
                    this.TextBox8.ReadOnly = true;
                    this.TextBox8.Text = datos["razonSocial"].ToString();
                    this.TextBox9.ReadOnly = true;

                    DropDownList1.Visible = false;
                    Button4.Visible = false;
                    Button3.Enabled = true;

                    this.TBNroDNI.BackColor = System.Drawing.Color.White;
                    this.TBNombre.BackColor = System.Drawing.Color.White;
                    this.TBApellido.BackColor = System.Drawing.Color.White;
                    this.DropTipoDNI.BackColor = System.Drawing.Color.White;
                    this.TBNroTel.BackColor = System.Drawing.Color.White;
                    this.DropTipoTel.BackColor = System.Drawing.Color.White;
                    this.TBEmail.BackColor = System.Drawing.Color.White;
                    this.TBObservaciones.BackColor = System.Drawing.Color.White;

                    this.TBCalle.BackColor = System.Drawing.Color.White;
                    this.TBNroCalle.BackColor = System.Drawing.Color.White;
                    this.TBPiso.BackColor = System.Drawing.Color.White;
                    this.TBDpto.BackColor = System.Drawing.Color.White;
                    this.TBCodPostal.BackColor = System.Drawing.Color.White;
                    this.DropBarrio.BackColor = System.Drawing.Color.White;
                    this.DropLocalidad.BackColor = System.Drawing.Color.White;
                    this.DropProvincia.BackColor = System.Drawing.Color.White;


                    Session["cuit"] = null;
                }
            }
        }

        if (Session["id"] != null)
        {
            if (!Page.IsPostBack)
            {
               
                this.Label7.Text = Session["id"].ToString();
                this.DSContacto.SelectParameters["id"].DefaultValue = this.Label7.Text;
                this.DSContacto.DataSourceMode = SqlDataSourceMode.DataReader;
                SqlDataReader datos = (SqlDataReader)this.DSContacto.Select(DataSourceSelectArguments.Empty);
                Session["id"] = null;
                Session["cuit"] = null;

                if (datos.Read())
                {
                    TextBox8.Text = Session["razon"].ToString().ToUpper();
                    TBNombre.Text = Session["nombre"].ToString().ToUpper();
                    TBApellido.Text = Session["apellido"].ToString().ToUpper();
                    TBNroDNI.Text = Session["dni"].ToString().ToUpper();
                    
                    TBNroTel.Text = Session["telefono"].ToString().ToUpper();
                    TBObservaciones.Text = Session["observaciones"].ToString();
                    TBCalle.Text = Session["calle"].ToString().ToUpper();
                    TBNroCalle.Text = Session["numero"].ToString().ToUpper();
                    TBPiso.Text = Session["piso"].ToString().ToUpper();
                    TBDpto.Text = Session["dpto"].ToString().ToUpper();
                    TBCodPostal.Text = Session["codigo"].ToString().ToUpper();
                    TBEmail.Text = Session["mail"].ToString();
                    //DropBarrio.SelectedValue = Session["barrio"].ToString();
                    //DropLocalidad.SelectedValue = Session["localidad"].ToString();
                    //DropProvincia.SelectedValue = Session["provincia"].ToString();

                    this.TBNroDNI.ReadOnly = true;
                    this.TBNombre.ReadOnly = true;
                    this.TBApellido.ReadOnly = true;
                    this.DropTipoDNI.Enabled = false;
                    this.TBNroTel.ReadOnly = true;
                    this.DropTipoTel.Enabled = false;
                    this.TBEmail.ReadOnly = true;
                    this.TBObservaciones.ReadOnly = true;

                    this.TBCalle.ReadOnly = true;
                    this.TBNroCalle.ReadOnly = true;
                    this.TBPiso.ReadOnly = true;
                    this.TBDpto.ReadOnly = true;
                    this.TBCodPostal.ReadOnly = true;
                    this.DropBarrio.Enabled = false;
                    this.DropLocalidad.Enabled = false;
                    this.DropProvincia.Enabled = false;
                   

                    this.TextBox8.ReadOnly = true;
                    this.TextBox9.ReadOnly = true;
                    this.Button5.Enabled = true;


                    this.TBNroDNI.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.TBNombre.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.TBApellido.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.DropTipoDNI.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.TBNroTel.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.DropTipoTel.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.TBEmail.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.TBObservaciones.BackColor = System.Drawing.Color.FromName("#DDEDEE");

                    this.TBCalle.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.TBNroCalle.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.TBPiso.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.TBDpto.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.TBCodPostal.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.DropBarrio.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.DropLocalidad.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    this.DropProvincia.BackColor = System.Drawing.Color.FromName("#DDEDEE");

                   

                }
            }
        }

                      

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmEmpresa.aspx");
    }


    protected void Button3_Click(object sender, EventArgs e)
    {

        Label5.Text = "";     
        this.DSContacto2.SelectParameters["numDocumento"].DefaultValue = this.TBNroDNI.Text;
             this.DSContacto2.DataSourceMode = SqlDataSourceMode.DataReader;
             SqlDataReader datos = (SqlDataReader)this.DSContacto2.Select(DataSourceSelectArguments.Empty);

             if (datos.Read() == true)
             {
                 Label6.Text = "El DNI Ingresado ya Existe";
                 TBNroDNI.Focus();

             }
             else
             {
                 this.DSContacto.InsertParameters["calle"].DefaultValue = this.TBCalle.Text.ToUpper();
                 this.DSContacto.InsertParameters["numeroCalle"].DefaultValue = this.TBNroCalle.Text.ToUpper();
                 this.DSContacto.InsertParameters["piso"].DefaultValue = this.TBPiso.Text.ToUpper();
                 this.DSContacto.InsertParameters["departamento"].DefaultValue = this.TBDpto.Text.ToUpper();
                 this.DSContacto.InsertParameters["codPostal"].DefaultValue = this.TBCodPostal.Text;
                 this.DSContacto.InsertParameters["idBarrio"].DefaultValue = this.DropBarrio.SelectedValue;
                 this.DSContacto.InsertParameters["idLocalidad"].DefaultValue = this.DropLocalidad.SelectedValue;
                 this.DSContacto.InsertParameters["idProvincia"].DefaultValue = this.DropProvincia.SelectedValue;


                 this.DSContacto.InsertParameters["cuit"].DefaultValue = this.TextBox9.Text.ToUpper();
                 this.DSContacto.InsertParameters["numDocumento"].DefaultValue = this.TBNroDNI.Text.ToUpper();
                 this.DSContacto.InsertParameters["nombre"].DefaultValue = this.TBNombre.Text.ToUpper();
                 this.DSContacto.InsertParameters["apellido"].DefaultValue = this.TBApellido.Text.ToUpper();
                 this.DSContacto.InsertParameters["dirCorreo"].DefaultValue = this.TBEmail.Text;
                 this.DSContacto.InsertParameters["numTelefono"].DefaultValue = this.TBNroTel.Text;
                 this.DSContacto.InsertParameters["idTipoTelefono"].DefaultValue = this.DropTipoTel.SelectedValue;
                 this.DSContacto.InsertParameters["idTipoDocumento"].DefaultValue = this.DropTipoDNI.SelectedValue;
                 this.DSContacto.InsertParameters["observaciones"].DefaultValue = this.TBObservaciones.Text;

                 
                 this.DSContacto.Insert();


                 Label5.Text = "Los Datos se Registraron Correctamente";

                 this.TBNroDNI.ReadOnly = true;
                 this.TBNombre.ReadOnly = true;
                 this.TBApellido.ReadOnly = true;
                 this.DropTipoDNI.Enabled = false;
                 this.TBNroTel.ReadOnly = true;
                 this.DropTipoTel.Enabled = false;
                 this.TBEmail.ReadOnly = true;
                 this.TBObservaciones.ReadOnly = true;

                 this.TBCalle.ReadOnly = true;
                 this.TBNroCalle.ReadOnly = true;
                 this.TBPiso.ReadOnly = true;
                 this.TBDpto.ReadOnly = true;
                 this.TBCodPostal.ReadOnly = true;
                 this.DropBarrio.Enabled = false;
                 this.DropLocalidad.Enabled = false;
                 this.DropProvincia.Enabled = false;


             }
    }
    


    protected void Button4_Click(object sender, EventArgs e)
    {

        Button3.Enabled = true;
        this.Label4.Text = this.DropDownList1.SelectedValue.ToString();
                
        this.DSEmpresa.SelectParameters["cuit"].DefaultValue = this.Label4.Text;
        this.DSEmpresa.DataSourceMode = SqlDataSourceMode.DataReader;
        SqlDataReader datos = (SqlDataReader)this.DSEmpresa.Select(DataSourceSelectArguments.Empty);

        if (datos.Read())
        {
            this.TextBox8.Text = datos["nombreComercial"].ToString().ToUpper();
            this.TextBox9.Text = datos["cuit"].ToString().ToUpper();
            
            this.TextBox8.ReadOnly = true;
            this.TextBox9.ReadOnly = true;
        }

        this.TBNroDNI.BackColor = System.Drawing.Color.White;
        this.TBNombre.BackColor = System.Drawing.Color.White;
        this.TBApellido.BackColor = System.Drawing.Color.White;
        this.DropTipoDNI.BackColor = System.Drawing.Color.White;
        this.TBNroTel.BackColor = System.Drawing.Color.White;
        this.DropTipoTel.BackColor = System.Drawing.Color.White;
        this.TBEmail.BackColor = System.Drawing.Color.White;
        this.TBObservaciones.BackColor = System.Drawing.Color.White;

        this.TBCalle.BackColor = System.Drawing.Color.White;
        this.TBNroCalle.BackColor = System.Drawing.Color.White;
        this.TBPiso.BackColor = System.Drawing.Color.White;
        this.TBDpto.BackColor = System.Drawing.Color.White;
        this.TBCodPostal.BackColor = System.Drawing.Color.White;
        this.DropBarrio.BackColor = System.Drawing.Color.White;
        this.DropLocalidad.BackColor = System.Drawing.Color.White;
        this.DropProvincia.BackColor = System.Drawing.Color.White;

        this.TBNroDNI.ReadOnly = false;
        this.TBNombre.ReadOnly = false;
        this.TBApellido.ReadOnly = false;
        this.TBNroTel.ReadOnly = false;
        this.TBEmail.ReadOnly = false;
        this.TBObservaciones.ReadOnly = false;

        this.TBCalle.ReadOnly = false;
        this.TBNroCalle.ReadOnly = false;
        this.TBPiso.ReadOnly = false;
        this.TBDpto.ReadOnly = false;
        this.TBCodPostal.ReadOnly = false;

        
        this.TBNroDNI.Enabled = true;
        this.TBNombre.Enabled = true;
        this.TBApellido.Enabled = true;
        this.DropTipoDNI.Enabled = true;
        this.TBNroTel.Enabled = true;
        this.DropTipoTel.Enabled = true;
        this.TBEmail.Enabled = true;
        this.TBObservaciones.Enabled = true;

        this.TBCalle.Enabled = true;
        this.TBNroCalle.Enabled = true;
        this.TBPiso.Enabled = true;
        this.TBDpto.Enabled = true;
        this.TBCodPostal.Enabled = true;
        this.DropBarrio.Enabled = true;
        this.DropLocalidad.Enabled = true;
        this.DropProvincia.Enabled = true;
           


        Label6.Text = "";

       Button4.Visible = false;
       DropDownList1.Visible = false;
                
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        Label6.Text = "";

        if (Button5.Text.Equals("Modificar Datos"))
        {
            Label5.Text = "";
            this.TBNroDNI.ReadOnly = false;
            this.TBNombre.ReadOnly = false;
            this.TBApellido.ReadOnly = false;
            this.TBNroTel.ReadOnly = false;
            this.TBEmail.ReadOnly = false;
            this.TBObservaciones.ReadOnly = false;

            this.TBCalle.ReadOnly = false;
            this.TBNroCalle.ReadOnly = false;
            this.TBPiso.ReadOnly = false;
            this.TBDpto.ReadOnly = false;
            this.TBCodPostal.ReadOnly = false;
           

            this.TextBox8.ReadOnly = true;
            this.TextBox9.ReadOnly = true;

            this.TBNroDNI.Enabled = false;
            this.TBNombre.Enabled = true;
            this.TBApellido.Enabled = true;
            this.DropTipoDNI.Enabled = true;
            this.TBNroTel.Enabled = true;
            this.DropTipoTel.Enabled = true;
            this.TBEmail.Enabled = true;
            this.TBObservaciones.Enabled = true;

            this.TBCalle.Enabled = true;
            this.TBNroCalle.Enabled = true;
            this.TBPiso.Enabled = true;
            this.TBDpto.Enabled = true;
            this.TBCodPostal.Enabled = true;
            this.DropBarrio.Enabled = true;
            this.DropLocalidad.Enabled = true;
            this.DropProvincia.Enabled = true;
           


            Button5.Text = "Guardar Cambios";
            Button5.Font.Bold = true;

        }

        else
        {
          
                 this.DSContacto.UpdateParameters["calle"].DefaultValue = this.TBCalle.Text.ToUpper();
                 this.DSContacto.UpdateParameters["numeroCalle"].DefaultValue = this.TBNroCalle.Text.ToUpper();
                 this.DSContacto.UpdateParameters["piso"].DefaultValue = this.TBPiso.Text.ToUpper();
                 this.DSContacto.UpdateParameters["departamento"].DefaultValue = this.TBDpto.Text.ToUpper();
                 this.DSContacto.UpdateParameters["codPostal"].DefaultValue = this.TBCodPostal.Text;
                 this.DSContacto.UpdateParameters["idBarrio"].DefaultValue = this.DropBarrio.SelectedValue;
                 this.DSContacto.UpdateParameters["idLocalidad"].DefaultValue = this.DropLocalidad.SelectedValue;
                 this.DSContacto.UpdateParameters["idProvincia"].DefaultValue = this.DropProvincia.SelectedValue;


                 this.DSContacto.UpdateParameters["cuit"].DefaultValue = this.TextBox9.Text;
                 this.DSContacto.UpdateParameters["numDocumento"].DefaultValue = this.TBNroDNI.Text;
                 this.DSContacto.UpdateParameters["nombre"].DefaultValue = this.TBNombre.Text.ToUpper();
                 this.DSContacto.UpdateParameters["apellido"].DefaultValue = this.TBApellido.Text.ToUpper();
                 this.DSContacto.UpdateParameters["dirCorreo"].DefaultValue = this.TBEmail.Text;
                 this.DSContacto.UpdateParameters["numTelefono"].DefaultValue = this.TBNroTel.Text.ToUpper();
                 this.DSContacto.UpdateParameters["idTipoTelefono"].DefaultValue = this.DropTipoTel.SelectedValue;
                 this.DSContacto.UpdateParameters["idTipoDocumento"].DefaultValue = this.DropTipoDNI.SelectedValue;
                 this.DSContacto.UpdateParameters["observaciones"].DefaultValue = this.TBObservaciones.Text;

               
                     this.DSContacto.Update();


                     Label5.Text = "Los Datos se Modificaron Correctamente";

                     this.TBNroDNI.ReadOnly = true;
                     this.TBNombre.ReadOnly = true;
                     this.TBApellido.ReadOnly = true;
                     this.DropTipoDNI.Enabled = false;
                     this.TBNroTel.ReadOnly = true;
                     this.DropTipoTel.Enabled = false;
                     this.TBEmail.ReadOnly = true;
                     this.TBObservaciones.ReadOnly = true;

                     this.TBCalle.ReadOnly = true;
                     this.TBNroCalle.ReadOnly = true;
                     this.TBPiso.ReadOnly = true;
                     this.TBDpto.ReadOnly = true;
                     this.TBCodPostal.ReadOnly = true;
                     this.DropBarrio.Enabled = false;
                     this.DropLocalidad.Enabled = false;
                     this.DropProvincia.Enabled = false;

                     this.TBNroDNI.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.TBNombre.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.TBApellido.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.DropTipoDNI.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.TBNroTel.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.DropTipoTel.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.TBEmail.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.TBObservaciones.BackColor = System.Drawing.Color.FromName("#DDEDEE");

                     this.TBCalle.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.TBNroCalle.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.TBPiso.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.TBDpto.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.TBCodPostal.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.DropBarrio.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.DropLocalidad.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                     this.DropProvincia.BackColor = System.Drawing.Color.FromName("#DDEDEE");
                    
                     Label6.Text = "";


                     Button5.Text = "Modificar Datos";
                 }
                    
        
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
       
        Button5.Text = "Modificar Datos"; ;
        Button5.Enabled = false;
        Label6.Text = "";
        Label5.Text = "";

        this.TextBox8.Text = "";
        this.TextBox9.Text = "";
        DropDownList1.Visible = true;
        Button4.Visible = true;

        this.TBNroDNI.Text="";
        this.TBNombre.Text = "";
        this.TBApellido.Text = "";
        this.DropTipoDNI.SelectedIndex = 0;
        this.TBNroTel.Text = "";
        this.DropTipoTel.SelectedIndex = 0;
        this.TBEmail.Text = "";
        this.TBObservaciones.Text = "";

        this.TBCalle.Text = "";
        this.TBNroCalle.Text = "";
        this.TBPiso.Text = "";
        this.TBDpto.Text = "";
        this.TBCodPostal.Text = "";
        this.DropBarrio.SelectedIndex = 0;
        this.DropLocalidad.SelectedIndex = 0;
        this.DropProvincia.SelectedIndex = 0;

        this.TBNroDNI.Enabled = true;
        this.TBNombre.Enabled = true;
        this.TBApellido.Enabled = true;
        this.DropTipoDNI.Enabled = true;
        this.TBNroTel.Enabled = true;
        this.DropTipoTel.Enabled = true;
        this.TBEmail.Enabled = true;
        this.TBObservaciones.Enabled = true;

        this.TBCalle.Enabled = true;
        this.TBNroCalle.Enabled = true;
        this.TBPiso.Enabled = true;
        this.TBDpto.Enabled = true;
        this.TBCodPostal.Enabled = true;
        this.DropBarrio.Enabled = true;
        this.DropLocalidad.Enabled = true;
        this.DropProvincia.Enabled = true;


        this.TBNroDNI.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.TBNombre.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.TBApellido.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.DropTipoDNI.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.TBNroTel.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.DropTipoTel.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.TBEmail.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.TBObservaciones.BackColor = System.Drawing.Color.FromName("#DDEDEE");

        this.TBCalle.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.TBNroCalle.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.TBPiso.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.TBDpto.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.TBCodPostal.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.DropBarrio.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.DropLocalidad.BackColor = System.Drawing.Color.FromName("#DDEDEE");
        this.DropProvincia.BackColor = System.Drawing.Color.FromName("#DDEDEE");

        Label6.Text = "";
        


    }
    protected void Button7_Click(object sender, EventArgs e)
    {
       
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmListaDeContactos.aspx");
    }
}